#include "Animation.h"
#include "General.h"
#include "Task.h"
#include "Task_LCfg.h"
#include "MCU.h"
#include "SysCommon.h"


/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Variables
 ******************************************************************************/
AnmPlayerState_t g_stAnmPlayerState = PLAYER_STATE_INIT_VALUE;

/*Functions********************************************************************/
void Anm_ResetPlayPara(AnmPlayPara_t* pstpara)
{
	pstpara->pstAnmTable = NULL;
	pstpara->u16AnmTableSize = 0;
	pstpara->pfStartCallback  = NULL;
	pstpara->pfFinishCallback = NULL;
	pstpara->pfStopCallback = NULL;
}

/*Functions********************************************************************/
const AnmStep_t* Anm_GetCurrentAnmTable()
{
	if(g_stAnmPlayerState.pstPlayPara != NULL)
	{
		return g_stAnmPlayerState.pstPlayPara->pstAnmTable;
	}
	return NULL;
}

/*Functions********************************************************************/
void Anm_Play(AnmPlayPara_t* pstplayPara)
{
	if(ANMPLAYER_STATE_STOP == g_stAnmPlayerState.u8PlayerState || PWM_LightShow.PWM_LS_Flag == ON)
	{
		g_stAnmPlayerState.u16AnmStepIndex = 0;
		g_stAnmPlayerState.pstPlayPara = (const AnmPlayPara_t*)pstplayPara;
		FUNC_CALL(pstplayPara->pfStartCallback);
        Task_SetCurrTaskTime( 0, 20, ENABLE);
		g_stAnmPlayerState.u8PlayerState = ANMPLAYER_STATE_PLAYING;
	}
	PWM_LightShow.PWM_LS_Sts = ON;
}

/*Functions********************************************************************/
void Anm_Finish()
{
	if(ANMPLAYER_STATE_PLAYING == g_stAnmPlayerState.u8PlayerState)
	{

        Task_SetCurrTaskTime( 50, 20, DISABLE);
		FUNC_CALL(g_stAnmPlayerState.pstPlayPara->pfFinishCallback);
		g_stAnmPlayerState.u8PlayerState = ANMPLAYER_STATE_FINISH;
		PWM_Capture_HV.State = PWM_NORMAL;
        g_stLinReqCmd.AnimationCtrl = 0;

        if(PWM_Capture_HV.mode == 4 && !PWM_LightShow.PWM_LS_SW)
        {
            g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime = 300;
            g_u8CurWorkMode = 0;
        }
        if(PWM_Capture_HV.mode == 5 && !PWM_LightShow.PWM_LS_SW)
        {
            g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime = 800;
            g_u8CurWorkMode = 0;
        }
        if(PWM_Capture_HV.mode == 6 && !PWM_LightShow.PWM_LS_SW)
        {
            g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime = 1400;
            g_u8CurWorkMode = 0;
        }

        PWM_Capture_HV.mode = 0;
	}
	PWM_LightShow.PWM_LS_Sts = OFF;
}

/*Functions********************************************************************/
void Anm_Stop()
{
	if(ANMPLAYER_STATE_STOP != g_stAnmPlayerState.u8PlayerState)
	{
		FUNC_CALL(g_stAnmPlayerState.pstPlayPara->pfStopCallback);
		g_stAnmPlayerState.u16AnmStepIndex = 0;
		g_stAnmPlayerState.pstPlayPara = NULL;
		g_stAnmPlayerState.u8PlayerState = ANMPLAYER_STATE_STOP;
	}
}

/*Functions********************************************************************/
void Task_ProcessAnmStep()
{

    if(PWM_LightShow.PWM_LS_SW)
    {
    	g_u8LS_Start_Wait = OFF;
    }

    if(g_stTaskList[TASK_INDEX_ANMPLAY].u8TaskEnFlag)
    {
        uint8_t u8Loop = 0;
        uint16_t u16Duty_Chn = 0;
        uint8_t u8Chn = 0;
        const AnmStep_t* pstAnmStep = NULL;

            for(u8Loop=0; u8Loop<0xFF; u8Loop++)
            {
                pstAnmStep = (const AnmStep_t*)&g_stAnmPlayerState.pstPlayPara->pstAnmTable[g_stAnmPlayerState.u16AnmStepIndex];
                u16Duty_Chn = pstAnmStep->u16PwmDuty_Chn;
                u8Chn = pstAnmStep->u8Chn;
                LED_SetDuty(pstAnmStep->u8Dev, u8Chn, u16Duty_Chn);
                if((g_stAnmPlayerState.u16AnmStepIndex+1) ==  g_stAnmPlayerState.pstPlayPara->u16AnmTableSize)
                {
                    Anm_Finish();
                    g_stTaskList[TASK_INDEX_INPUT].u8TaskEnFlag = DISABLE;
                    g_stTaskList[TASK_INDEX_INPUT].u32DelayTime = 0;
                    g_u8LS_Start_Wait = ON;
                }
                else
                {
                    g_stAnmPlayerState.u16AnmStepIndex += 1;
                    if(pstAnmStep->u16TimeDelay != 0)
                    {
                        Task_SetCurrTaskTime(((pstAnmStep->u16TimeDelay) * ANM_TIME_DELAY_SCALE), 30, ENABLE);      // ANMPLAY_TIME_SET
                        break;
                    }
                }
            }

    }

}
